package com.example.mvp.mvppromote.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.mvp.mvppromote.R;
import com.example.mvp.mvppromote.model.BandItemBean;
import com.example.mvp.mvppromote.ui.activity.WebViewActivity;
import com.example.mvp.mvppromote.utils.GlideCircleTransform;
import com.example.mvp.mvppromote.utils.RxBus;
import com.example.mvp.mvppromote.utils.RxBusEvent;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by wty on 2016/6/23 0023.
 */
public class BandAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    @BindView(R.id.text)
    TextView text;
    private ArrayList<BandItemBean> bandItemBeens;
    private Context context;
    private boolean visi = false;
    private static final int TYPE_NORMAL_ITEM = 0;  //普通Item
    private static final int TYPE_FOOTER_ITEM = 1;  //底部FooterView

    public BandAdapter(ArrayList<BandItemBean> bandItemBeens, Context context) {
        this.bandItemBeens = bandItemBeens;
        this.context = context;
    }

    public void setVisi(boolean visi) {
        this.visi = visi;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (viewType == TYPE_NORMAL_ITEM) {
            view = LayoutInflater.from(context).inflate(R.layout.grid_item, null);
            return new MyViewHolder(view);
        } else {
            view = LayoutInflater.from(context).inflate(R.layout.bottomlayout, null);
            return new BottomViewHolder(view);
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof MyViewHolder) {
            MyViewHolder viewHolder = (MyViewHolder) holder;
            Glide.with(holder.itemView.getContext()).load(bandItemBeens.get(position).getSmallImage()).transform(new GlideCircleTransform(context, 6)).into(viewHolder.imageIv);
            viewHolder.descriptionTv.setText(bandItemBeens.get(position).getSummary());
            viewHolder.descriptionTime.setText(bandItemBeens.get(position).getTime());
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    RxBus.getInstance().send(new RxBusEvent(position));
                    Intent intent = new Intent(context, WebViewActivity.class);
                    intent.putExtra("id", bandItemBeens.get(position).getId());
                    intent.putExtra("title", bandItemBeens.get(position).getTitle());
                    context.startActivity(intent);
                }
            });
        }
        if (holder instanceof BottomViewHolder) {
            if (visi) {
                ((BottomViewHolder) holder).text.setVisibility(View.VISIBLE);
            } else {
                ((BottomViewHolder) holder).text.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER_ITEM;
        } else {
            return TYPE_NORMAL_ITEM;
        }
    }

    @Override
    public int getItemCount() {
        return bandItemBeens.size() + 1;
    }


    static class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.imageIv)
        ImageView imageIv;
        @BindView(R.id.descriptionTv)
        TextView descriptionTv;
        @BindView(R.id.descriptionTime)
        TextView descriptionTime;

        MyViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    static class BottomViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.text)
        TextView text;

        BottomViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
